#include "main.h"
#include "system.h"
signed short GetTemperature(uint32 R)
{
	signed long R1 = 0, R2 = 0;
	signed long T1 = 0, T2 = 0, T = 0;
	if (R >= 174600)
	{
		return -35;
	}
	else if (R >= 134470)
	{
		T1 = -35;
		T2 = -30;
		R1 = 174600;
		R2 = 134470;
	}
	else if (R >= 104210)
	{
		T1 = -30;
		T2 = -25;
		R1 = 134470;
		R2 = 104210;
	}
	else if (R >= 81450)
	{
		T1 = -25;
		T2 = -20;
		R1 = 104210;
		R2 = 81450;
	}
	else if (R >= 63550)
	{
		T1 = -20;
		T2 = -15;
		R1 = 81450;
		R2 = 63550;
	}
	else if (R >= 49200)
	{
		T1 = -15;
		T2 = -10;
		R1 = 63550;
		R2 = 49200;
	}
	else if (R >= 39070)
	{
		T1 = -10;
		T2 = -5;
		R1 = 49200;
		R2 = 39070;
	}
	else if (R >= 31210)
	{
		T1 = -5;
		T2 = 0;
		R1 = 39070;
		R2 = 31210;
	}
	else if (R >= 24640)
	{
		T1 = 0;
		T2 = 5;
		R1 = 31210;
		R2 = 24640;
	}
	else if (R >= 19689)
	{
		T1 = 5;
		T2 = 10;
		R1 = 24640;
		R2 = 19689;
	}
	else if (R >= 15864)
	{
		T1 = 10;
		T2 = 15;
		R1 = 19689;
		R2 = 15864;
	}
	else if (R >= 12653)
	{
		T1 = 15;
		T2 = 20;
		R1 = 15864;
		R2 = 12653;
	}
	else if (R >= 10000)
	{
		T1 = 20;
		T2 = 25;
		R1 = 12653;
		R2 = 10000;
	}
	else if (R >= 8087)
	{
		T1 = 25;
		T2 = 30;
		R1 = 10000;
		R2 = 8087;
	}
	else if (R >= 6566)
	{
		T1 = 30;
		T2 = 35;
		R1 = 8087;
		R2 = 6566;
	}
	else if (R >= 5229)
	{
		T1 = 35;
		T2 = 40;
		R1 = 6566;
		R2 = 5229;
	}
	else if (R >= 4369)
	{
		T1 = 40;
		T2 = 45;
		R1 = 5229;
		R2 = 4369;
	}
	else if (R >= 3588)
	{
		T1 = 45;
		T2 = 50;
		R1 = 4369;
		R2 = 3588;
	}
	else if (R >= 2975)
	{
		T1 = 50;
		T2 = 55;
		R1 = 3588;
		R2 = 2975;
	}
	else if (R >= 2482)
	{
		
		T1 = 55;
		T2 = 60;
		R1 = 2975;
		R2 = 2482;
	}
	else if (R >= 2070)
	{
		T1 = 60;
		T2 = 65;
		R1 = 2482;
		R2 = 2070;
	}
	else if (R >= 1748)
	{
		T1 = 65;
		T2 = 70;
		R1 = 2070;
		R2 = 1748;
	}
	else if (R >= 1476)
	{
		T1 = 70;
		T2 = 75;
		R1 = 1748;
		R2 = 1476;
	}
	else if (R >= 1231)
	{
		T1 = 75;
		T2 = 80;
		R1 = 1476;
		R2 = 1231;
	}
	else if (R >= 1044)
	{
		T1 = 80;
		T2 = 85;
		R1 = 1231;
		R2 = 1044;
	}
	else if (R >= 890)
	{
		T1 = 85;
		T2 = 90;
		R1 = 1044;
		R2 = 890;
	}
	else if (R >= 775)
	{
		T1 = 90;
		T2 = 95;
		R1 = 890;
		R2 = 775;
	}
	else if (R >= 650)
	{
		T1 = 95;
		T2 = 100;
		R1 = 775;
		R2 = 650;
	}
	else if (R >= 559)
	{
		T1 = 100;
		T2 = 105;
		R1 = 650;
		R2 = 559;
	}
	else if (R >= 481)
	{
		T1 = 105;
		T2 = 110;
		R1 = 559;
		R2 = 481;
	}
	else if (R >= 415)
	{
		T1 = 110;
		T2 = 115;
		R1 = 481;
		R2 = 415;
	}
	else if (R >= 363)
	{
		T1 = 115;
		T2 = 120;
		R1 = 415;
		R2 = 363;
	}
	else if (R >= 313)
	{
		T1 = 120;
		T2 = 125;
		R1 = 363;
		R2 = 313;
	}
	else if (R >= 273)
	{
		T1 = 125;
		T2 = 130;
		R1 = 313;
		R2 = 273;
	}
	else if (R >= 240)
	{
		
		T1 = 130;
		T2 = 135;
		R1 = 273;
		R2 = 240;
	}
	else if (R >= 211)
	{
		
		T1 = 135;
		T2 = 140;
		R1 = 240;
		R2 = 211;
	}
	else if (R >= 186)
	{
		T1 = 140;
		T2 = 145;
		R1 = 211;
		R2 = 186;
	}
	else if (R >= 163)
	{
		T1 = 145;
		T2 = 150;
		R1 = 186;
		R2 = 163;
	}
	else
	{
		return 150;
	}
	
	/**********
		T1=55;
		T2=60;
		R1=2975;
		R2=2482;

		2607
		
	printf("T1=%d",T1);
	printf("T2=%d",T2);
	printf("R1=%d",R1);
	printf("R2=%d",R2);
	************/
	T = R - R2;
	T = 10000 * T * (T1 - T2);
	T = T / (R1 - R2);
	T = T + 10000 * T2;
	
	
	return T / 1000;
}

void AnalysisSampleData(SampledDataAnalysis *data)
{
	uint8 i = 0;
	uint32 ta_avrager = 0;
	uint32 tb_avrager = 0;
	uint32 tc_avrager = 0;
	uint32 td_avrager = 0;

	memset((uint8 *)data, 0, sizeof(SampledDataAnalysis));

	data->Ua_max = AdcValBuf[0].Ua;
	data->Ua_min = AdcValBuf[0].Ua;

	data->Ub_max = AdcValBuf[0].Ub;
	data->Ub_min = AdcValBuf[0].Ub;

	data->Uc_max = AdcValBuf[0].Uc;
	data->Uc_min = AdcValBuf[0].Uc;

	data->Ia_max = AdcValBuf[0].Ia;
	data->Ia_min = AdcValBuf[0].Ia;

	data->Ib_max = AdcValBuf[0].Ib;
	data->Ib_min = AdcValBuf[0].Ib;

	data->Ic_max = AdcValBuf[0].Ic;
	data->Ic_min = AdcValBuf[0].Ic;

	data->Iabc_max = AdcValBuf[0].Iabc;
	data->Iabc_min = AdcValBuf[0].Iabc;

	for (i = 0; i < Max_Adc_Time; i++)
	{

		
		data->Ua_Effective += (AdcValBuf[i].Ua - AdcValBuf[i].Vr / 2) * (AdcValBuf[i].Ua - AdcValBuf[i].Vr / 2);
		data->Ub_Effective += (AdcValBuf[i].Ub - AdcValBuf[i].Vr / 2) * (AdcValBuf[i].Ub - AdcValBuf[i].Vr / 2);
		data->Uc_Effective += (AdcValBuf[i].Uc - AdcValBuf[i].Vr / 2) * (AdcValBuf[i].Uc - AdcValBuf[i].Vr / 2);
		
		data->Ia_Effective += (AdcValBuf[i].Ia - AdcValBuf[i].Vr / 2) * (AdcValBuf[i].Ia - AdcValBuf[i].Vr / 2);
		data->Ib_Effective += (AdcValBuf[i].Ib - AdcValBuf[i].Vr / 2) * (AdcValBuf[i].Ib - AdcValBuf[i].Vr / 2);
		data->Ic_Effective += (AdcValBuf[i].Ic - AdcValBuf[i].Vr / 2) * (AdcValBuf[i].Ic - AdcValBuf[i].Vr / 2);
		
		data->Iabc_Effective += (AdcValBuf[i].Iabc - AdcValBuf[i].Vr / 2) * (AdcValBuf[i].Iabc - AdcValBuf[i].Vr / 2);
		
		ta_avrager += AdcValBuf[i].Ta;
		tb_avrager += AdcValBuf[i].Tb;
		tc_avrager += AdcValBuf[i].Tc;
		td_avrager += AdcValBuf[i].Td;
	}

	/*******************************************
	����һ���ź���Чֵ�Ķ���: ʱ������˲ʱֵ�ڸ���ʱ������
	�ľ�����ֵ��������������ʱ����Ϊһ������
	*******************************************/
	
	
	
	data->Ua_Effective = sqrt(data->Ua_Effective / Max_Adc_Time) * 22044 / 10000;
	data->Ub_Effective = sqrt(data->Ub_Effective / Max_Adc_Time) * 22044 / 10000;
	data->Uc_Effective = sqrt(data->Uc_Effective / Max_Adc_Time) * 22044 / 10000;

	
	
	data->Ia_Effective = sqrt(data->Ia_Effective / Max_Adc_Time) * 3000000 / 12977;
	data->Ib_Effective = sqrt(data->Ib_Effective / Max_Adc_Time) * 3000000 / 12977;
	data->Ic_Effective = sqrt(data->Ic_Effective / Max_Adc_Time) * 3000000 / 12977;

	
	
	data->Iabc_Effective = sqrt(data->Iabc_Effective /Max_Adc_Time) * 2000 / 989;

	ta_avrager = ta_avrager / Max_Adc_Time;
	tb_avrager = tb_avrager / Max_Adc_Time;
	tc_avrager = tc_avrager / Max_Adc_Time;
	td_avrager = td_avrager / Max_Adc_Time;

	if (ta_avrager > 500) 
	{
		data->Ta_average = GetTemperature((4096 * FixedResistanceValue) / ta_avrager - FixedResistanceValue);
	}

	if (tb_avrager > 500)
	{
		data->Tb_average = GetTemperature((4096 * FixedResistanceValue) / tb_avrager - FixedResistanceValue);
	}
	if (tc_avrager > 500)
	{	
		data->Tc_average = GetTemperature((4096 * FixedResistanceValue) / tc_avrager - FixedResistanceValue);
	}
	if (td_avrager > 500)
	{
		data->Td_average = GetTemperature((4096 * FixedResistanceValue) / td_avrager - FixedResistanceValue);
	}
}
void Output_AdcVal(void)
{
	uint16_t i = 0, Vdd = 0;
	 
	SampledDataAnalysis sampledresult = {0};
	for (i = 0; i < Max_Adc_Time; i++)
	{
		Vdd = VREFINT_CAL * 3300 / AdcValBuf[i].Vr;
		
		AdcValBuf[i].Ua = (uint32)AdcValBuf[i].Ua * Vdd / 4095;
		AdcValBuf[i].Ub = (uint32)AdcValBuf[i].Ub * Vdd / 4095;
		AdcValBuf[i].Uc = (uint32)AdcValBuf[i].Uc * Vdd / 4095;

		AdcValBuf[i].Ia = (uint32)AdcValBuf[i].Ia * Vdd / 4095;
		AdcValBuf[i].Ib = (uint32)AdcValBuf[i].Ib * Vdd / 4095;
		AdcValBuf[i].Ic = (uint32)AdcValBuf[i].Ic * Vdd / 4095;
		AdcValBuf[i].Iabc = (uint32)AdcValBuf[i].Iabc*Vdd / 4095;

		AdcValBuf[i].Vr = Vdd;

	}

	
	AnalysisSampleData(&sampledresult);

	
	

#if 0
	printf("Ua_max=%d Ua_min=%d effect=%dV\n", sampledresult.Ua_max, sampledresult.Ua_min, sampledresult.Ua_EffectiveValue);
	printf("--->>Ua=%dv\n", sampledresult.Ua_Effective);
	printf("Ub_max=%d Ub_min=%d effect=%dV\n", sampledresult.Ub_max, sampledresult.Ub_min, sampledresult.Ub_EffectiveValue);
	printf("--->>Ub=%dv\n", sampledresult.Ub_Effective);
	printf("Uc_max=%d Uc_min=%d effect=%dV\n", sampledresult.Uc_max, sampledresult.Uc_min, sampledresult.Uc_EffectiveValue);
	printf("--->>Uc=%dv\n", sampledresult.Uc_Effective);
	
	printf("Ta=%d\n", sampledresult.Ta_average);
	printf("Tb=%d\n", sampledresult.Tb_average);
	printf("Tc=%d\n", sampledresult.Tc_average);
	printf("Td=%d\n", sampledresult.Td_average);
	
	printf("Ia_max=%d Ia_min=%d effect=%dma\n", sampledresult.Ia_max, sampledresult.Ia_min, sampledresult.Ia_EffectiveValue);
	printf("--->>Ia=%dma\n", sampledresult.Ia_Effective);
	printf("Ib_max=%d Ib_min=%d effect=%dma\n", sampledresult.Ib_max, sampledresult.Ib_min, sampledresult.Ib_EffectiveValue);
	printf("--->>Ib=%dma\n", sampledresult.Ib_Effective);
	printf("Ic_max=%d Ic_min=%d effect=%dma\n", sampledresult.Ic_max, sampledresult.Ic_min, sampledresult.Ic_EffectiveValue);
	printf("--->>Ic=%dma\n", sampledresult.Ic_Effective);
	printf("Iabc_max=%d Iabc_min=%d effect=%dma\n", sampledresult.Iabc_max, sampledresult.Iabc_min, sampledresult.Iabc_EffectiveValue);
	printf("--->>Iabc=%dma\n\n", sampledresult.Iabc_Effective);
#endif

	collectData.CurData.aVal=sampledresult.Ia_Effective/100;
	
	collectData.CurData.bVal=sampledresult.Ib_Effective/100;
	collectData.CurData.cVal=sampledresult.Ic_Effective/100;
	collectData.CurData.rVal=sampledresult.Iabc_Effective;

	

	collectData.VolData.aVal=sampledresult.Ua_Effective;
	collectData.VolData.bVal=sampledresult.Ub_Effective;
	collectData.VolData.cVal=sampledresult.Uc_Effective;

	collectData.TemData.aVal=sampledresult.Ta_average;
	collectData.TemData.bVal=sampledresult.Tb_average;
	collectData.TemData.cVal=sampledresult.Tc_average;	
	collectData.TemData.sVal=sampledresult.Td_average;
}
